package com.andong.controller;

import com.andong.annotations.AccessControl;
import com.andong.domain.dto.AdminDTO;
import com.andong.domain.dto.UpdatePasswordDTO;
import com.andong.domain.pojo.PageResult;
import com.andong.domain.pojo.Result;
import com.andong.service.AdminService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.time.LocalDate;

import static com.andong.utils.SystemConstant.*;
import static com.andong.utils.SystemConstant.SUPER_ADMIN;

/**
 * 管理员信息表(Admin)表控制层
 *
 * @author makejava
 * @since 2024-01-03 18:43:52
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    @GetMapping("/{id}")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN})
    public Result selectById(@PathVariable Long id) {
        return adminService.selectById(id);
    }

    @PutMapping("/updatePassByCode")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN})
    public Result updatePasswordByCode(@RequestBody UpdatePasswordDTO dto) throws Exception {
        return adminService.updatePasswordByCode(dto);
    }

    @PutMapping("/updatePassByPre")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN})
    public Result updatePasswordByPre(@RequestBody UpdatePasswordDTO dto) throws Exception {
        return adminService.updatePasswordByPre(dto);
    }

    @GetMapping("/list")
    @AccessControl(roles = SUPER_ADMIN)
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        PageResult pageResult = adminService.queryList(page, pageSize, name, gender, begin, end);
        return Result.success(pageResult);
    }

    @PutMapping("/image")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN})
    public Result updateImage(String newUrl) {
        adminService.updateImage(newUrl);
        return Result.success();
    }

    @PutMapping("/phone")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN})
    public Result updatePhone(String phone, String code) {
        return adminService.updatePhone(phone, code);
    }

    @PutMapping("/status")
    @AccessControl(roles = SUPER_ADMIN)
    public Result updateStatus(Long id, Integer status) {
        System.out.println(status);
        return adminService.updateStatus(id, status);
    }

    @PostMapping("/add")
    @AccessControl(roles = SUPER_ADMIN)
    public Result insert(@RequestBody AdminDTO dto) throws Exception {
        return adminService.insert(dto);
    }

    @DeleteMapping("/{id}")
    @AccessControl(roles = SUPER_ADMIN)
    public Result delete(@PathVariable Long id) {
        return adminService.delete(id);
    }
}

